package com.cuijq.ms.command.v1;

import com.cuijq.ms.command.v1.impl.ArchiveCommand;
import com.cuijq.ms.command.v1.impl.GotDiamondCommand;
import com.cuijq.ms.command.v1.impl.GotStartCommand;
import com.cuijq.ms.command.v1.impl.HitObstacleCommand;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author by CuiJQ
 * @Classname GameApplication
 * @Description GameApplication Description
 * @Date 2021/5/31 11:58
 */
public class GameApplication {

  private static final int MAX_HANDLED_REQ_COUNT_PER_LOOP = 100;
  private static Queue<Command> queue = new LinkedList<>();

  public static void main(String[] args) {
    while (true) {
      List<Request> requests = new ArrayList<>();
      //省略从epoll或者select中获取数据，并封装成Request的逻辑，
      // 注意设置超时时间，如果很长时间没有接收到请求，就继续下面的逻辑处理。
      for (Request request : requests) {
        Event event = request.getEvent();
        Command command = null;
        if (event.equals(Event.GOT_DIAMOND)) {
          command = new GotDiamondCommand("Data");
        } else if (event.equals(Event.GOT_STAR)) {
          command = new GotStartCommand("Data");
        } else if (event.equals(Event.HIT_OBSTACLE)) {
          command = new HitObstacleCommand("Data");
        } else if (event.equals(Event.ARCHIVE)) {
          command = new ArchiveCommand("Data");
        } // ...一堆else if...
        queue.add(command);
      }
      int handledCount = 0;
      while (handledCount < MAX_HANDLED_REQ_COUNT_PER_LOOP) {
        if (queue.isEmpty()) {
          break;
        }
        Command command = queue.poll();
        command.execute();
      }
    }
  }
}
